<?php

	$filename = "";
	$message = "";
	$backupSuccessful = false;
	try {
		$emailaddress = "sabelo@mzansicharts.co.za";
		$host="localhost"; // database host
		$dbuser="thnoteco_finance"; // database user name
		$dbpswd="Musn12nat"; // database password
		$mysqldb="thnoteco_finance"; // name of database
		$path = "/home/thnoteco/public_html/sabelo"; // full server path to the directory where you want the backup files (no trailing slash)
		// modify the above values to fit your environment
		$filename = $path . "/backup" . date("d") . ".sql";
		if ( file_exists($filename) ) unlink($filename);
		system("mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb > $filename",$result);
		$size = filesize($filename);
		switch ($size) {
		  case ($size>=1048576): $size = round($size/1048576) . " MB"; break;
		  case ($size>=1024): $size = round($size/1024) . " KB"; break;
		  default: $size = $size . " bytes"; break;
		}
		
		//$message = "The database backup for " . $mysqldb . " has been run.\n\n";
		//$message .= "The return code was: " . $result . "\n\n";
		//$message .= "The file path is: " . $filename . "\n\n";
		//$message .= "Size of the backup: " . $size . "\n\n";
		//$message .= "Server time of the backup: " . date(" F d h:ia") . "\n\n";
	
		$backupSuccessful = true;
	} catch (Exception $e){
		$message = "The database backup for " . $mysqldb . " could not be run!\n\n ".$e->getMessage();
		mail($emailaddress, "Database Backup Message", "Databse on GRS has not been run successfully today.\n\n". $e->getMessage());
		return;
	}

	if (!$backupSuccessful) {
		mail($emailaddress, "Database Backup Message", "Databse on GRS has not been run successfully today.");
	}
	
	echo "success: ".$backupSuccessful;
?>